import csv

def clean_header(header):
    """清理表头，删除最后一个字母或符号后面的内容，并去除所有的换行符和多余的空格"""
    cleaned_header = []
    for column in header:
        # 删除换行符和多余的空格
        cleaned_column = column.replace('\n', '').replace('\r', '').strip()
        # 删除最后一个字母或符号后面的内容
        cleaned_column = cleaned_column.rstrip(" \n\r\t")
        cleaned_header.append(cleaned_column)
    return cleaned_header

def process_csv(input_file, output_file):
    """读取CSV文件并处理表头"""
    with open(input_file, mode='r', encoding='utf-8') as infile:
        reader = csv.reader(infile)
        headers = next(reader)
        cleaned_headers = clean_header(headers)
        
        # 写入新的CSV文件
        with open(output_file, mode='w', newline='', encoding='utf-8') as outfile:
            writer = csv.writer(outfile)
            writer.writerow(cleaned_headers)
            # 写入其余行
            for row in reader:
                writer.writerow(row)

# 使用示例
input_file = 'input.csv'   # 输入文件路径
output_file = 'output.csv'  # 输出文件路径
process_csv(input_file, output_file)

print(f"Processed CSV saved as {output_file}")
